﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Forms.DataVisualization.Charting;

namespace HabitAnalyzer
{
    /// <summary>
    /// Interaction logic for CategoryComparison.xaml
    /// </summary>
    
    public partial class CategoryComparison : Window
    {
        DbUtility db = new DbUtility();
        public struct Totaler
        {
            public Category _cat;
            public TimeSpan _span;
            public string _catName;

            public TimeSpan span
            { get { return _span; } }

            public string catName
            { get { return _catName; } }
        }

        public struct PassedData
        {
            public DateTime start;
            public DateTime end;
            public int sessionid;
            public string title;
            public List<Category> cats;
        }

        PassedData init;

        ObservableCollection<Totaler> _ActInfo = new ObservableCollection<Totaler>();
        public CategoryComparison(DateTime start, DateTime end, int sessionid, string title, List<Category> cats)
        {
            InitializeComponent();
            init = new PassedData();
            init.start = start;
            init.end = end;
            init.sessionid = sessionid;
            init.title = title;
            init.cats = cats;
            DbUtility db = new DbUtility();
            this.Title = title;
            int i,j, count = cats.Count, acount;
            for (i = 0; i < count; i++)
            {
                List<SessionActivity> acts = db.GetActivitiesBetweenDatesByCategory(sessionid, start, end, cats.ElementAt(i).CategoryID);
                acount = acts.Count;
                TimeSpan total = TimeSpan.Zero;
                for (j = 0; j < acount; j++)
                {
                    total += acts.ElementAt(j).timeDifference;
                }
                Totaler tot = new Totaler();
                tot._cat = db.GetCategoryById(cats.ElementAt(i).CategoryID).Single();
                tot._span = total;
                tot._catName = tot._cat.Name;
                if (tot._span != TimeSpan.Zero)
                    _ActInfo.Add(tot);
                Console.WriteLine(tot._catName);
            }
            if (_ActInfo.Count==0)
            {
                label1.Visibility = Visibility.Visible;
                table.Visibility = Visibility.Hidden;
                CategoryDetail.Visibility = Visibility.Hidden;
            }

            //awful bubble sort
            for(i=0; i<_ActInfo.Count; i++)
                for (j=0; j<_ActInfo.Count-1; j++)
                    if (_ActInfo.ElementAt(j)._span < _ActInfo.ElementAt(j + 1)._span)
                    {
                        Totaler temp = _ActInfo.ElementAt(j);
                        _ActInfo[j] = _ActInfo[j + 1];
                        _ActInfo[j + 1] = temp;
                    }

            //System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
            Chart chart = new Chart();
            chart.ChartAreas.Add("Default");
            
            Series series = new Series();
            //series.Points.Add(5);
            //series.Points.Add(10);
            series.ChartType=SeriesChartType.Pie;
            for (i = 0; i < _ActInfo.Count; i++)
                series.Points.AddXY(_ActInfo[i]._catName, _ActInfo[i]._span.Ticks);
            chart.Series.Add(series);
            //chart.Legends.Add("Default");
            chart.BackColor = System.Drawing.Color.FromArgb(249, 249, 249);
            chart.ChartAreas[0].BackColor = System.Drawing.Color.FromArgb(249, 249, 249);
            host.Child = chart;

            
                        

            // Add the chart to the grid so it can be displayed.
            //this.grid1.Children.Add(host);


            
        }
        public ObservableCollection<Totaler> ActInfo
        { get { return _ActInfo; } }

        private void CategoryDetail_Click(object sender, RoutedEventArgs e)
        {
            if (table.SelectedIndex == -1)
            {
                System.Windows.Forms.MessageBox.Show("Please select a category from the table.");
                return;
            }
            List<SessionActivity> sa = db.GetActivitiesBetweenDatesByCategorySorted(init.sessionid, init.start, init.end, _ActInfo[table.SelectedIndex]._cat.CategoryID);
            Console.WriteLine("TaDa!");
            Console.WriteLine(_ActInfo[table.SelectedIndex]._cat.Name);
            Window report = new BasicReport(sa, "Category Detail Report");
            report.Show();
        }
    }
}
